//
//  ChatPersonViewController.m
//  QuanZiChat
//
//  Created by lanou3g on 15/12/1.
//  Copyright © 2015年 ZhongHongLin. All rights reserved.
//

#import "ChatPersonViewController.h"
#import "ChatPersonViewController.h"
#import "KxMenu.h"
#import "QRCodeGenerator.h"
#import <ZBarSDK.h>
#import "JZAddFriendViewController.h"
#import "JZCreateGroupViewController.h"
#import <AVOSCloudIM.h>


@interface ChatPersonViewController ()<ZBarReaderDelegate>

@end

@implementation ChatPersonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor greenColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"找你玩" style:(UIBarButtonItemStylePlain) target:self action:@selector(clickBarButtonAction:)];
    
    //设置聊天类型
    [self setDisplayConversationTypeArray:@[@(ConversationType_PRIVATE)]];
    // 收到消息通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveMessage:) name:RCKitDispatchMessageNotification object:nil];
    //取消中间的分割线
    [self.conversationListTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //设置title
    self.navigationItem.title = [NSString stringWithFormat:@"%@的会话",[AVUser currentUser].username];
}

- (IBAction)selectButton:(id)sender {
    NSArray * menuArray = @[[KxMenuItem menuItem:@"添加好友"
                                           image:nil//[UIImage imageNamed:@"action_icon"]
                                          target:self
                                          action:@selector(pushToAddFriend:)],
                            
                            [KxMenuItem menuItem:@"扫一扫"
                                           image:nil//[UIImage imageNamed:@"check_icon"]
                                          target:self
                                          action:@selector(pushToScansion:)],
                            
                            [KxMenuItem menuItem:@"创建群组"
                                           image:nil//[UIImage imageNamed:@"reload"]
                                          target:self
                                          action:@selector(pushToCreation:)]];
    CGRect frame =CGRectMake(self.view.frame.size.width - 50, 20, 45, 50);
    [KxMenu showMenuInView:self.view
                  fromRect:frame
                 menuItems:menuArray];
}

-(void)pushToAddFriend:(id)sender{
    JZAddFriendViewController *addFriendVC = [self.storyboard instantiateViewControllerWithIdentifier:@"JZAddFriendViewController"];
    [self.navigationController pushViewController:addFriendVC animated:YES];
}

-(void)pushToScansion:(id)sender{
    //创建扫描界面
    ZBarReaderViewController *readVC = [self.storyboard instantiateViewControllerWithIdentifier:@"ZBarReaderViewController"];
    readVC.readerDelegate = self;
    //设置支持方向
    readVC.supportedOrientationsMask = ZBarOrientationMaskAll;
    //设置图片扫描器
    ZBarImageScanner *scanner = readVC.scanner;
    //配置扫描器
    [scanner setSymbology:ZBAR_I25 config:ZBAR_CFG_ENABLE to:0];
    
    [self showViewController:readVC sender:nil];
}

-(void)pushToCreation:(id)sender{
    
    JZCreateGroupViewController *createGroupVC = [self.storyboard instantiateViewControllerWithIdentifier:@"JZCreateGroupViewController"];
    [self.navigationController pushViewController:createGroupVC animated:YES];
    
}




- (void)receiveMessage:(NSNotification *)sender{
    [UIApplication sharedApplication].applicationIconBadgeNumber += 1;
}

//每一个会话的点击事件
- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType conversationModel:(RCConversationModel *)model atIndexPath:(NSIndexPath *)indexPath{
    // 创建一个聊天界面对象
    RCConversationViewController *conversationVC = [RCConversationViewController new];
    conversationVC.conversationType = model.conversationType;
    conversationVC.targetId = model.targetId;
    conversationVC.userName = model.conversationTitle;
    conversationVC.title = model.conversationTitle;
    conversationVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:conversationVC animated:YES];
}


-(void)clickBarButtonAction:(UIBarButtonItem *)barButtonItem{
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
